package com.ruoyi.gis.earthai.service;

import com.aliyun.aiearth_engine20220609.Client;
import com.aliyun.aiearth_engine20220609.models.*;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.gis.domain.DatasetRaster;
import com.ruoyi.gis.domain.DatasetRasterCollected;
import com.ruoyi.gis.earthai.vo.CollectDataVo;
import com.ruoyi.gis.earthai.vo.CreateAiJobVo;
import com.ruoyi.gis.service.IDatasetRasterBandsService;
import com.ruoyi.gis.service.IDatasetRasterCollectedService;
import com.ruoyi.gis.service.IDatasetRasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

//https://engine-aiearth.aliyun.com/docs/page/api?d=04c9d6#heading-1
@Service
public class EarthDataApiService {
    @Autowired
    private EarthAiService earthAiService;
    @Autowired
    private IDatasetRasterService datasetRasterService;
    @Autowired
    private IDatasetRasterCollectedService datasetRasterCollectedService;
    /**
     * https://engine-aiearth.aliyun.com/docs/page/api?d=04c9d6#heading-85
     * @param sourceTypeList  星源
     * @param regionWkt  区域选择，标准wkt格式
     * @param dateStart  开始日期，例如： 2020-01-01
     * @param dateEnd  结束日期
     * @param cloudageMin 云量下限，取值0-100，默认0
     * @param cloudageMax 云量上限，取值0-100，默认100
     * @param pageSize 每页数量
     * @param pageNumber 页码
     */
    public ListDatasResponseBody listDatas(List<String> sourceTypeList,
                          String regionWkt,
                          String dateStart,
                          String dateEnd,
                          Integer cloudageMin,
                          Integer cloudageMax,
                          Integer pageSize,
                          Integer pageNumber
                          ){
        try {
            Client client = earthAiService.getClient();
            ListDatasRequest request = new ListDatasRequest();
            request.setCloudageMax(cloudageMax);
            request.setCloudageMin(cloudageMin);
            request.setDateEnd(dateEnd);
            request.setDateStart(dateStart);
            request.setPageNumber(pageNumber);
            request.setPageSize(pageSize);
            request.setRegionWkt(regionWkt);
            request.setSourceTypeList(sourceTypeList);
            ListDatasResponse response = client.listDatas(request);
            //System.out.println(gson.toJson(listDatasResponse.getBody()));
            ListDatasResponseBody body = response.getBody();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public DownloadDataResponseBody downlaodData(String dataId){
        try {
            Client client = earthAiService.getClient();
            DownloadDataRequest request = new DownloadDataRequest();
            request.setDataId(dataId);
            DownloadDataResponse response = client.downloadData(request);
            //System.out.println(gson.toJson(listDatasResponse.getBody()));
            DownloadDataResponseBody body = response.getBody();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public CreateAIJobResponseBody createAIJob(CreateAiJobVo createAiJobVo){
        try {
            Client client = earthAiService.getClient();
            CreateAIJobRequest request = new CreateAIJobRequest();
            request.setApp("");
            CreateAIJobResponse response = client.createAIJob(request);
            //System.out.println(gson.toJson(listDatasResponse.getBody()));
            CreateAIJobResponseBody body = response.getBody();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public GetJobsResponseBody getAIJob(CreateAiJobVo createAiJobVo){
        try {
            Client client = earthAiService.getClient();
            GetJobsRequest request = new GetJobsRequest();
            request.setJobIds(Collections.singletonList(2835L));
            GetJobsResponse  response = client.getJobs(request);
            //System.out.println(gson.toJson(listDatasResponse.getBody()));
            GetJobsResponseBody body = response.getBody();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public DeleteJobsResponseBody deleteAIJob(CreateAiJobVo createAiJobVo){
        try {
            Client client = earthAiService.getClient();
            DeleteJobsRequest request = new DeleteJobsRequest();
            request.setJobIds(Collections.singletonList(2835L));
            DeleteJobsResponse  response = client.deleteJobs(request);
            //System.out.println(gson.toJson(listDatasResponse.getBody()));
            DeleteJobsResponseBody body = response.getBody();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void datasetRasterCollectService(CollectDataVo collectDataVo, LoginUser loginUser) {
        DatasetRaster raster = datasetRasterService.selectDatasetRasterByDataId(collectDataVo.getDataId());
        if(raster==null)return;
        DatasetRasterCollected collect = datasetRasterCollectedService.
                selectUserCollect(collectDataVo.getDataId(),loginUser.getUserId());
        if(collect==null){
            collect = new DatasetRasterCollected();
            collect.setUserId(loginUser.getUserId());
            collect.setDataId(collectDataVo.getDataId());
            collect.setRasterId(raster.getId());
            collect.setCreateBy(loginUser.getUserId()+"");
            datasetRasterCollectedService.insertDatasetRasterCollected(collect);
        }
    }
}
